package com.hiray.taglayoutmanager;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestManager;
import com.hiray.taglayoutmanager.uitility.ClickableViewHolder;

import java.util.Arrays;
import java.util.List;

/**
 * Created by hiray on 2017/12/13.
 *
 * @author hiray
 */

public class ImageAdapter extends RecyclerView.Adapter<ImageAdapter.ViewHolder> {

    private Context context;
    private LayoutInflater layoutInflater;
    private RequestManager requestManager;
//    Random random = new Random(System.currentTimeMillis());

    private List<String> imageUrls = Arrays.asList(
            "https://ss0.baidu.com/7Po3dSag_xI4khGko9WTAnF6hhy/image/h%3D300/sign=eab54beb99ef76c6cfd2fd2bad17fdf6/9e3df8dcd100baa1c279de714e10b912c9fc2ea6.jpg",
            "https://ss0.baidu.com/7Po3dSag_xI4khGko9WTAnF6hhy/image/h%3D300/sign=6b4548e4db1b0ef473e89e5eedc551a1/b151f8198618367a15337f1727738bd4b21ce546.jpg",
            "https://ss3.baidu.com/9fo3dSag_xI4khGko9WTAnF6hhy/image/h%3D300/sign=0637c5e533292df588c3aa158c335ce2/9345d688d43f8794361b48e4db1b0ef41ad53a17.jpg",
            "https://ss2.baidu.com/-vo3dSag_xI4khGko9WTAnF6hhy/image/h%3D300/sign=198ab26eadc27d1eba263dc42bd7adaf/3812b31bb051f8194bad0380d3b44aed2f73e774.jpg",
            "https://ss1.baidu.com/-4o3dSag_xI4khGko9WTAnF6hhy/image/h%3D300/sign=d4f4ff7a6c380cd7f91ea4ed9145ad14/ca1349540923dd5432c4a7a2d809b3de9d824840.jpg",
            "https://ss0.baidu.com/7Po3dSag_xI4khGko9WTAnF6hhy/image/h%3D300/sign=5bc9dd173e9b033b3388fada25cf3620/77c6a7efce1b9d1672195a72f9deb48f8d5464c3.jpg",
            "https://ss3.baidu.com/9fo3dSag_xI4khGko9WTAnF6hhy/image/h%3D300/sign=86cb376eadc27d1eba263dc42bd7adaf/3812b31bb051f819d4ec8680d3b44aed2f73e72b.jpg",
            "https://ss1.baidu.com/-4o3dSag_xI4khGko9WTAnF6hhy/image/h%3D300/sign=ce977272dd3f8794ccff4e2ee2190ead/728da9773912b31bec835ce38f18367adbb4e109.jpg",
            "https://ss1.baidu.com/9vo3dSag_xI4khGko9WTAnF6hhy/image/h%3D300/sign=ce79df02134c510fb1c4e41a50582528/b8389b504fc2d562cc08d127ee1190ef77c66cf3.jpg",
            "https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=1925629126,540735551&fm=27&gp=0.jpg",
            "https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=1400616962,1483047440&fm=27&gp=0.jpg",
            "https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=1982722608,4096501668&fm=27&gp=0.jpg",
            "https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=948124355,4130291333&fm=27&gp=0.jpg",
            "https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=151902301,1969857578&fm=27&gp=0.jpg",
            "https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=974885309,3931392404&fm=27&gp=0.jpg",
            "https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=149911146,2633361781&fm=11&gp=0.jpg",
            "https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=1560971882,2607620609&fm=27&gp=0.jpg",
            "https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=2431669048,453009335&fm=27&gp=0.jpg",
            "https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=3280596539,2264408850&fm=27&gp=0.jpg",
            "https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2273070347,106577595&fm=27&gp=0.jpg",
            "https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=2290295350,381602053&fm=27&gp=0.jpg");

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (layoutInflater == null) {
            context = parent.getContext();
            layoutInflater = LayoutInflater.from(context);
            requestManager = Glide.with(context);
        }
        int layoutId = context.getResources().getIdentifier("simple_image" + (viewType % 2 == 0 ? "" : "1"), "layout", context.getPackageName());
        return new ViewHolder(layoutInflater.inflate(layoutId,parent, false));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        requestManager.load(imageUrls.get(position))
                .fitCenter()
                .into(holder.image);
        holder.text.setText(String.valueOf(holder.getAdapterPosition()));
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return imageUrls.size();
    }

    class ViewHolder extends ClickableViewHolder {

        ImageView image;
        TextView text;

         ViewHolder(View itemView) {
            super(itemView);
            image = itemView.findViewById(R.id.image);
            text = itemView.findViewById(R.id.text);
        }
    }

    @Override
    public boolean onFailedToRecycleView(ViewHolder holder) {
        return super.onFailedToRecycleView(holder);
    }

    @Override
    public void onViewRecycled(ViewHolder holder) {
        holder.image.setImageDrawable(null);
    }
}
